package com.zhz.lib.cms.manage.hotlink;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Hibernate;
import org.hibernate.classic.Session;
import org.springframework.beans.BeanUtils;

import com.zhz.lib.cms.domain.hotlink.hotlink;
import com.zhz.lib.cms.domain.type.Sortmap;
import com.zhz.lib.cms.manage.BaseActionWithSearch;
import com.zhz.lib.cms.utils.zhz.ZhzSortMapUtil;
import com.zhz.lib.cms.vo.hotlink.HotlinkVO;
import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.common.util.hibernate.BaseDAO;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;
import com.zhz.project.common.util.zhzutil.ZhzStringUtil;

public class hotlinkAction extends BaseActionWithSearch {

    private static String TABLE = "hotlink";
    private static String KEY   = "id";
    private static String LIST  = "hotlinklist";
    //连接类型
    private Integer       hotlinkfatherid;

    /**
     * 不给参数，默认执行此处
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
     *      org.apache.struts.action.ActionForm,
     *      javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    @Override
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response)
                                                                                              throws Exception {

        return hotlinklist(mapping, form, request, response);
    }

    /**
     * 显示链接列表！！
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward hotlinklist(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response)
                                                                                              throws Exception {

        // 取出选择的链接类型
        String radiooption = ZhzHttpRequestUtil.getParameters(request, "radiooption");
        request.setAttribute("radiooption", radiooption);

        String whereStatement = BaseDAO.WHERE;
        StringBuffer fanyeHref = new StringBuffer();
        fanyeHref.append("hotlinkManage.do?method=hotlinklist");

        // 根据选择的链接类型选项进行查询
        if (StringUtil.equalsIgnoreCase(radiooption, "r")) {
            Sortmap smp = (Sortmap) baseDao.findOneObject("Sortmap", "type", "rlink");
            if (smp != null)
                whereStatement += "and type='" + smp.getSortMapId() + "'";
            fanyeHref.append("&type=rlink");

        } else if (StringUtil.equalsIgnoreCase(radiooption, "h")) {
            Sortmap smp = (Sortmap) baseDao.findOneObject("Sortmap", "type", "hlink");
            if (smp != null)
                whereStatement += "and type='" + smp.getSortMapId() + "'";
            fanyeHref.append("&type=hlink");
        }

        List<hotlink> hotlinklist = getListWithFanyeHref(request, TABLE, whereStatement,
            " order by sortNumber ", LIST, fanyeHref);

        List<HotlinkVO> hotlinkVO = new ArrayList<HotlinkVO>();
        if (hotlinklist != null && hotlinklist.size() > 0) {
            for (hotlink o : hotlinklist) {
                hotlinkVO.add(changeToVo(o));
            }
        }

        // 查询的结果集
        request.setAttribute(LIST, hotlinkVO);
        return mapping.findForward("hotlink-list");
    }

    /**
     * 将news转换为页面需要的数据
     * 
     * @param news
     */
    private HotlinkVO changeToVo(hotlink param) {
        HotlinkVO hotlinkVO = new HotlinkVO();
        BeanUtils.copyProperties(param, hotlinkVO);
        Sortmap smp = (Sortmap) baseDao.findOneObject("Sortmap", "sortMapId", param.getType());
        if (smp != null)
            hotlinkVO.setTypeString(smp.getValue());
        return hotlinkVO;
    }

    /**
     * 添加链接！！
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward hotlinkadd(ActionMapping mapping, ActionForm form,
                                    HttpServletRequest request, HttpServletResponse response) {

        request.setAttribute("hotlinksortlist", ZhzSortMapUtil.getChildSortMapList(hotlinkfatherid,
            baseDao));

        return mapping.findForward("hotlink-add");
    }

    /**
     * 保存新添加的链接！！
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward hotlinksave(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response) {

        String title = ZhzHttpRequestUtil.getParameters(request, "title");
        String url = ZhzHttpRequestUtil.getParameters(request, "url");
        String type = ZhzHttpRequestUtil.getParameters(request, "type");
        // 获取排序
        String sortNumber = ZhzHttpRequestUtil.getParameters(request, "sortNumber");

        hotlink hotlink = new hotlink();

        hotlink.setTitle(title);
        hotlink.setUrl(url);
        hotlink.setType(type);
        if (StringUtil.isNotBlank(sortNumber) && StringUtil.isNumeric(sortNumber)) {
            hotlink.setSortNumber(Integer.valueOf(sortNumber));
        } else {
            hotlink.setSortNumber(getMaxSortNumber(type));
        }
        boolean save = baseDao.save(hotlink);

        if (save) {
            request.setAttribute("addresult", "addresult");
            request.setAttribute("add", "add");
        }

        /**
         * 返回
         */
        return mapping.findForward("hotlink-result");

    }

    /**
     * 编辑链接
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward hotlinkedit(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response) {
        String hotlinkid = ZhzHttpRequestUtil.getParameters(request, "hotlinkid");

        if (ZhzStringUtil.isNotNull(hotlinkid)) {
            hotlink hotlink = (hotlink) baseDao.findOneObject("hotlink", "id", hotlinkid);
            request.setAttribute("hotlink", hotlink);
            request.setAttribute("sort", ZhzSortMapUtil.getSortmap(hotlink.getType(), baseDao));
            request.setAttribute("hotlinksortlist", ZhzSortMapUtil.getChildSortMapList(
                hotlinkfatherid, baseDao));
        }

        return mapping.findForward("hotlinkedit-form");
    }

    /**
     * 保存编辑后的链接
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward hotlinkeditsave(ActionMapping mapping, ActionForm form,
                                         HttpServletRequest request, HttpServletResponse response) {

        String hotlinkid = ZhzHttpRequestUtil.getParameters(request, "hotlinkid");
        String title = ZhzHttpRequestUtil.getParameters(request, "title");
        String url = ZhzHttpRequestUtil.getParameters(request, "url");
        String type = ZhzHttpRequestUtil.getParameters(request, "type");
        // 获取排序
        String sortNumber = ZhzHttpRequestUtil.getParameters(request, "sortNumber");

        if (ZhzStringUtil.isNotNull(hotlinkid)) {
            hotlink hotlink = (hotlink) baseDao.findOneObject("hotlink", "id", hotlinkid);

            hotlink.setTitle(title);
            hotlink.setUrl(url);
            hotlink.setType(type);
            if (StringUtil.isNotBlank(sortNumber) && StringUtil.isNumeric(sortNumber)) {
                hotlink.setSortNumber(Integer.valueOf(sortNumber));
            } else {
                hotlink.setSortNumber(getMaxSortNumber(type));
            }
            boolean update = baseDao.update(hotlink);

            if (update) {
                request.setAttribute("editresult", "editresult");
                request.setAttribute("edit", "edit");
            }
        }
        return mapping.findForward("hotlink-result");
    }

    /**
     * 删除链接
     */
    public ActionForward hotlinkdel(ActionMapping mapping, ActionForm form,
                                    HttpServletRequest request, HttpServletResponse response)
                                                                                             throws Exception {

        String hotlinkid = ZhzHttpRequestUtil.getParameters(request, "hotlinkid");

        if (ZhzStringUtil.isNotNull(hotlinkid)) {
            boolean delResult = baseDao.execute("delete from hotlink where id=" + hotlinkid);
            if (delResult) {
                request.setAttribute("del", "del");
                request.setAttribute("delResult", "delResult");
            }
        }
        /**
         * 返回
         */
        return mapping.findForward("hotlink-result");
    }

    private Integer getMaxSortNumber(String type) {
        String sql = "select max(sortNumber) sortNumber from hotlink where type='" + type + "'";
        Session session = baseDao.getHibernateTemplate().getSessionFactory().openSession();
        Integer maxId = (Integer) (session.createSQLQuery(sql).addScalar("sortNumber",
            Hibernate.INTEGER)).uniqueResult();
        // 比最大值步进10
        if (maxId != null) {
            return maxId + 10;
        } else {
            return 10;
        }
    }

    /**
     * Setter method for property <tt>hotlinkfatherid</tt>.
     * 
     * @param hotlinkfatherid value to be assigned to property hotlinkfatherid
     */
    public void setHotlinkfatherid(Integer hotlinkfatherid) {
        this.hotlinkfatherid = hotlinkfatherid;
    }

}
